package com.sky.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.dto.DishDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.admin.DishFlavorMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.service.admin.DishFlavorService;
import com.sky.service.admin.DishService;
import com.sky.utils.ChangeType;
import com.sky.utils.UUID;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service("adminDishService")
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    private final DishMapper dishMapper;
    private final DishFlavorMapper dishFlavorMapper;
    private final DishFlavorService dishFlavorService;


    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    @CacheEvict(cacheNames = "dish", key = "#dishDTO.categoryId")

    public void changeDish(DishDTO dishDTO) {
        DishVO dishVO1 = new DishVO();
        Dish dish = new Dish();
        DishVO dishVO = ChangeType.change(dishDTO, dishVO1);
        dishVO.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(dishVO, dish);
        dishMapper.updateById(dish);
        dishFlavorMapper.updateById(dishVO.getFlavors());
    }

    /**
     * 分页菜品
     * @param categoryId
     * @param name
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public Map<String, Object> findAllPage(Integer categoryId, String name, Integer page, Integer pageSize, Integer status) {
        Page<Dish> dishPage = new Page<>();
        dishPage.setCurrent(page);
        dishPage.setSize(pageSize);

        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(categoryId != null, "category_id", categoryId)
                    .like(name != null && !name.isEmpty(), "name", name)
                    .eq(status != null, "status", status);
        Page<Dish> dishPage1 = dishMapper.selectPage(dishPage, queryWrapper);

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("total", dishPage1.getTotal());
        stringObjectHashMap.put("records", dishPage1.getRecords());
        return stringObjectHashMap;
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getDishVoServiceById(Integer id) {
        Dish dishById = dishMapper.selectById(id);
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(new QueryWrapper<DishFlavor>().eq("dish_id", id));
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dishById, dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 添加菜品
     * @param dishDTO
     */
    @Override
    @CacheEvict(cacheNames = "dish", key = "#dishDTO.categoryId")
    public void addDish(DishDTO dishDTO) {
        dishDTO.setId(UUID.generateFromTimestamp());
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
//        dish.setStatus(1);
        dishDTO.getFlavors().forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
        dishMapper.insert(dish);
        dishFlavorService.saveBatch(dishDTO.getFlavors());
    }

    @Override
    @CacheEvict(cacheNames = "dish")
    public void deleteDishByIds(String ids) {
        List<String> list = Arrays.stream(ids.split(",")).toList();
        dishMapper.deleteByIds(list);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    @Cacheable(cacheNames = "dish", key = "#categoryId", unless = "#result == null || #result.isEmpty()")
    public List<Dish> findAllByCategoryId(Integer categoryId) {
        return dishMapper.selectList(new QueryWrapper<Dish>().eq("category_id", categoryId));
    }


}
